package com.sc.nft.dao;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.sc.nft.entity.DaoSchoolTop;
import com.sc.nft.entity.dto.DaoSchoolTopUpdateByIdAdminDTO;
import com.sc.nft.entity.vo.DaoSchoolTopHomeListVO;
import com.sc.nft.entity.vo.DaoSchoolTopPageAdminVO;
import com.sc.nft.mapper.DaoSchoolTopMapper;
import com.sc.nft.sup.PageRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * dao学堂的一级分类(DaoSchoolTop)表Dao层
 *
 * @author makejava
 * @since 2023-03-13 16:05:12
 */
@Slf4j
@Repository
public class DaoSchoolTopDao extends ServiceImpl<DaoSchoolTopMapper, DaoSchoolTop> {

    /**
     * 分页获取1级分类列表
     *
     * @param pageRequest
     * @return
     */
    public Page<DaoSchoolTopPageAdminVO> pageAdmin(PageRequest pageRequest) {
        return baseMapper.pageAdmin(pageRequest.toMp());
    }

    /**
     * @param name
     * @return
     */
    public Boolean selectByNameCountIsExist(String name) {
        QueryWrapper<DaoSchoolTop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        queryWrapper.eq("dao_id", 6L);
        Integer selectCount = baseMapper.selectCount(queryWrapper);
        return SqlHelper.retBool(selectCount);
    }

    /**
     * 判断除了自身id的name,其他的名字是否有重复
     *
     * @param name
     * @param id
     * @return
     */
    public Boolean selectByNameCountIsExistNotId(String name, Long id) {
        QueryWrapper<DaoSchoolTop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        queryWrapper.eq("dao_id", 6L);
        queryWrapper.ne("id", id);
        Integer selectCount = baseMapper.selectCount(queryWrapper);
        return SqlHelper.retBool(selectCount);
    }

    public Boolean updateById(DaoSchoolTopUpdateByIdAdminDTO daoSchoolTopUpdateByIdAdminDTO) {
        DaoSchoolTop info = new DaoSchoolTop();
        BeanUtil.copyProperties(daoSchoolTopUpdateByIdAdminDTO, info);
        return SqlHelper.retBool(baseMapper.updateById(info));
    }

    /**
     * 对应条目的数据+1
     *
     * @param id
     */
    public void totalAddOne(Long id) {
        UpdateWrapper<DaoSchoolTop> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.setSql("article_total=article_total+1");
        baseMapper.update(new DaoSchoolTop(), updateWrapper);
    }

    /**
     * 对应条目的数据-1
     *
     * @param id
     */
    public void totalSubOne(Long id) {
        UpdateWrapper<DaoSchoolTop> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.setSql("article_total=article_total-1");
        baseMapper.update(new DaoSchoolTop(), updateWrapper);
    }

    /**
     * 根据id获取唯一一条数据
     *
     * @param id
     * @return
     */
    public DaoSchoolTop selectById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 根据id删除指定数据
     *
     * @param id
     * @return
     */
    public Boolean deleteById(Long id) {
        return SqlHelper.retBool(baseMapper.deleteById(id));
    }

    /**
     * 获取全部数据
     *
     * @return
     */
    public List<DaoSchoolTopPageAdminVO> allAdmin() {
        return baseMapper.allAdmin();
    }

    /**
     * 首页获取父集列表
     *
     * @return
     */
    public Page<DaoSchoolTopHomeListVO> getHomeTopList(PageRequest pageRequest) {
        //2023年6月12日17:14:15  增加指定的daoId 6L是dao学堂
        return baseMapper.getHomeTopList(pageRequest.toMp(),6L);
    }

    /**
     * 对应条目的数据-sum
     *
     * @param id
     * @param sum
     */
    public void totalSubSum(Long id, Integer sum) {
        UpdateWrapper<DaoSchoolTop> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.setSql("article_total=article_total-" + sum);
        baseMapper.update(new DaoSchoolTop(), updateWrapper);
    }

    /**
     * 对应条目的数据+sum
     *
     * @param id
     * @param sum
     */
    public void totalAddSum(Long id, Integer sum) {
        UpdateWrapper<DaoSchoolTop> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.setSql("article_total=article_total+" + sum);
        baseMapper.update(new DaoSchoolTop(), updateWrapper);
    }

}
